package com.xinghou.platform.lucene.main.impl;

import com.xinghou.platform.dao.bases.util.StringUtils;
import com.xinghou.platform.dao.main.AreaCodeDAO;
import com.xinghou.platform.lucene.main.StoreLuceneService;
import com.xinghou.platform.lucene.main.bean.SolrServerFactory;
import com.xinghou.platform.lucene.main.bean.StoreSolr;
import com.xinghou.platform.lucene.main.bean.UserSolr;
import com.xinghou.platform.lucene.main.util.SolrJOper;
import com.xinghou.platform.lucene.main.util.SolrQuery;
import com.xinghou.platform.model.main.AreaCode;
import com.xinghou.platform.model.main.SearchPolymerization;
import com.xinghou.platform.model.main.Store;
import com.xinghou.platform.model.main.StoreQueryParam;
import com.xinghou.platform.page.Pagination;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.FacetField;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by qingshengzheng on 16/4/26.
 */
@Service("StoreSolrService")
public class StoreSolrServiceImpl implements StoreLuceneService {
    @Resource
    private AreaCodeDAO areaCodeDAO;

    private SolrJOper<StoreSolr> solrOper = new SolrJOper<StoreSolr>();
    private SolrServerFactory solrServerFactory = SolrServerFactory.instance();

    @Override
    public List<Store> getSearchStoreInfos(Boolean share, String key, int fromno, int pagesize, String City_id,double distinct, double lon, double lat) {
        HashMap map = new HashMap();
        SolrQuery query=new SolrQuery();
        if(share)
            query.between(SolrQuery.OPER_AND,"sharecount","1","*");
        query.like(SolrQuery.OPER_AND,"name", key).equal(SolrQuery.OPER_AND,"cityid",City_id).toString();
        String searchKey=query.toString();
        SolrClient server = solrServerFactory.getStoreServer();
        Pagination<StoreSolr> values = solrOper.queryLbsBean(server, searchKey, distinct, "position", lat + "," + lon, fromno, pagesize, StoreSolr.class);
        if (values==null||values.getValues() == null || values.getValues().isEmpty()) return null;
        List<Store> stores = new ArrayList<Store>();
        List<StoreSolr> solrs = values.getValues();
        for (int i = 0; i < solrs.size(); i++) {
            Store temp = solrs.get(i).toStore();
            stores.add(temp);
        }
        return stores;
    }


    @Override
    public List<SearchPolymerization> getSearchPolymerization(Boolean share, String key,double distinct, double lon, double lat,double level, String userType, String citycode) {
        HashMap map = new HashMap();
        SolrQuery query=new SolrQuery();
        List<SearchPolymerization> countList = new ArrayList<SearchPolymerization>();
        SolrClient server = null;
        if(share) {
            query.between(SolrQuery.OPER_AND, "sharecount", "1", "*");
            query.like(SolrQuery.OPER_AND, "name", key).equal(SolrQuery.OPER_AND, "cityid", null).toString();
            server = solrServerFactory.getStoreServer();
        }else{
            String searchKey = query.like(SolrQuery.OPER_AND,"nickname", key).equal(SolrQuery.OPER_AND,"citycode",citycode).toString();
             server = solrServerFactory.getUserServer();
        }
        String searchKey=query.toString();

        FacetField facetField = solrOper.queryCount(server, searchKey, distinct, "position", lat + "," + lon,level);
           String name = facetField.getName();
           int sda= facetField.getValueCount();
            List<FacetField.Count> list= facetField.getValues();

        for(FacetField.Count count: list){
            if(null==count|| StringUtils.isNullOrEmpty(count.getName())){
                continue;
            }
            SearchPolymerization searchPolymerization = new SearchPolymerization();
            AreaCode areaCode = areaCodeDAO.queryByAdcode(count.getName());
            if(null!=areaCode){
                String[] position =areaCode.getCenterxy().split(",");
                if(position.length==2){
                    searchPolymerization.setLongitude(position[0].trim());
                    searchPolymerization.setLatitude(position[1].trim());
                }
                searchPolymerization.setPosition(areaCode.getCenterxy());
                if(count.getCount()==0){
                    continue;
                }
                searchPolymerization.setCount(new Long(count.getCount()).intValue());
                countList.add(searchPolymerization);
            }
        }
        return countList;
    }


    @Override
    public List<Store> getSearchStoreInfos(String key, int fromno, int pagesize) throws IOException {
        SolrClient server = solrServerFactory.getStoreServer();
        Pagination<StoreSolr> values = solrOper.queryBean(server, key, fromno, pagesize, StoreSolr.class);
        if (values.getValues() == null || values.getValues().isEmpty()) return null;
        List<Store> stores = new ArrayList<Store>();
        List<StoreSolr> solrs = values.getValues();
        for (int i = 0; i < solrs.size(); i++) {
            Store temp = solrs.get(i).toStore();
            stores.add(temp);
        }
        return stores;
    }

    @Override
    public List<Store> queryStoreByParams(StoreQueryParam param,int fromno,int pagesize,double distinct ,double lon,double lat){
        SolrQuery solrQuery=new SolrQuery();
        if(param.categorycode!=null)
            solrQuery.equal("categorycode",param.categorycode+"");
        if(param.minsale!=null)
            solrQuery.between("consume",param.minsale+"",param.maxsale+"");
        String key=solrQuery.toString();
        SolrClient server = solrServerFactory.getStoreServer();
        Pagination<StoreSolr> values = solrOper.queryLbsBean(server,key,distinct,"position",lat+","+lon,fromno,pagesize,StoreSolr.class );
        if (values==null||values.getValues() == null || values.getValues().isEmpty()) return null;
        List<Store> stores = new ArrayList<Store>();
        List<StoreSolr> solrs = values.getValues();
        for (int i = 0; i < solrs.size(); i++) {
            Store temp = solrs.get(i).toStore();
            stores.add(temp);
        }
        return stores;


    }

    @Override
    public int saveStore(Store store) {
        SolrClient server = solrServerFactory.getStoreServer();
        solrOper.addBean(server, StoreSolr.fromStore(store));
        return 0;
    }

    @Override
    public int updateStore(Store store) {
        SolrClient server = solrServerFactory.getStoreServer();
        try {
            server.deleteById(store.getId() + "");
            solrOper.addBean(server, StoreSolr.fromStore(store));
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public boolean isExit(String Store_id) throws Exception {
        SolrClient server = solrServerFactory.getShareCardServer();
        Pagination pagination = solrOper.queryBean(server, "id:" + Store_id, 1, 10, StoreSolr.class);
        return !pagination.getValues().isEmpty();
    }
}